﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace EFChild
{
    //public delegate void DelegateChange();//声明一个委托
    public partial class page : DevExpress.XtraEditors.XtraUserControl
    {
        /// <summary>  
        /// 自定义事件 当前页改变事件  
        /// </summary>  
        public event EventHandler CurrentPageChange;

        private int m_recordCount = 0;
        /// <summary>  
        /// 数据行总数  
        /// </summary>  
        public int recordCount
        {
            get { return m_recordCount; }
            set
            {
                SetFormCtrlEnable();
                this.m_recordCount = value;
            }
        }

        
        /// <summary>  
        /// 每页显示的数据行数  
        /// </summary> 
        private int m_pageSize = 5; 
        public int pageSize
        {
            get { return m_pageSize; }
            set { this.m_pageSize = value; }
        }
        private int m_pageIndex = 1;
        /// <summary>  
        /// 当前页  
        /// </summary>  
        public int pageIndex
        {
            get { return m_pageIndex; }
            set { this.m_pageIndex = value; }
        }

        private int m_pageCount = 0;
        /// <summary>  
        /// 总页数  
        /// </summary>  
        public int pageCount
        {
            get
            {
                if (m_pageSize != 0)
                {
                    m_pageCount = GetPageCount();
                }
                return m_pageCount;
            }
        }
        /// <summary>  
        /// 获取总页数  
        /// </summary>  
        /// <returns></returns>  
        private int GetPageCount()
        {
            if (pageSize == 0)
            {
                return 0;
            }
            int num = 1;
            if (recordCount % pageSize == 0)
            {
                num = recordCount / pageSize;
            }
            else
            {
                num = recordCount / pageSize + 1;
            }
            return num;
        }
        public page()
        {
            InitializeComponent();
        }
        /// <summary>  
        /// 设置DataNavigator按钮全部可用  
        /// </summary>  
        private void SetFormCtrlEnable()
        {
            this.dataNavigator1.Buttons.CustomButtons[0].Enabled = true;
            this.dataNavigator1.Buttons.CustomButtons[1].Enabled = true;
            this.dataNavigator1.Buttons.CustomButtons[2].Enabled = true;
            this.dataNavigator1.Buttons.CustomButtons[3].Enabled = true;
        }  
        private void dataNavigator1_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            ShowEvent("ButtonClick", e.Button);         
        }
        /// <summary>  
        /// 第一页、上一页、下一页、最后一页 点击事件  
        /// </summary>  
        /// <param name="eventString"></param>  
        /// <param name="nbb"></param>  
        void ShowEvent(string eventString, NavigatorButtonBase nbb)
        {
            switch (nbb.Tag.ToString())
            {
                case "First":
                    First();
                    break;
                case "PrePage":
                    PrePage();
                    break;
                case "NextPage":
                    NextPage();
                    break;
                case "Last":
                    Last();
                    break;
                default:
                    break;
            }
            DrawControl(true);
        }

        private void First()
        {
            m_pageIndex = 1;
        }
        private void PrePage()
        {
            m_pageIndex = Math.Max(1, m_pageIndex - 1);
        }
        private void NextPage()
        {
            m_pageIndex = Math.Min(pageCount, m_pageIndex + 1);
        }
        private void Last()
        {
            m_pageIndex = pageCount;
        }
        /// <summary>  
        /// 设置DataNavigator按钮是否可用  
        /// </summary>  
        /// <param name="callEvent">是否当前页</param>  
        private void DrawControl(bool callEvent)
        {
            if (callEvent && CurrentPageChange != null)
            {
                //当前页事件  
                CurrentPageChange(this, null);//CurrentPageChange();
            }
            //设置按钮全部可用  
            this.SetFormCtrlEnable();
            //只有一页  
            if (recordCount <= 1)
            {
                this.dataNavigator1.Buttons.CustomButtons[0].Enabled = false;
                this.dataNavigator1.Buttons.CustomButtons[1].Enabled = false;
                this.dataNavigator1.Buttons.CustomButtons[2].Enabled = false;
                this.dataNavigator1.Buttons.CustomButtons[3].Enabled = false;
            }//第一页  
            else if (pageIndex <= 1)
            {
                this.dataNavigator1.Buttons.CustomButtons[0].Enabled = false;
                this.dataNavigator1.Buttons.CustomButtons[1].Enabled = false;
            }//最后一页  
            else if (pageIndex >= pageCount)
            {
                this.dataNavigator1.Buttons.CustomButtons[2].Enabled = false;
                this.dataNavigator1.Buttons.CustomButtons[3].Enabled = false;
            }

        }

        /// <summary>  
        /// 加载LableControl  
        /// </summary>  
        private void ShowLableInfo()
        {
            if (!this.Created)
            {
                return;
            }
            this.BeginInvoke((MethodInvoker)delegate
            {
                try
                {
                    if (!DesignMode)
                    {
                        this.labelControl1.Text = "总行数：" + recordCount + "      当前页：" + pageIndex + "/" + pageCount + "    ";
                    }
                }
                catch { }
            });
        }

        /// <summary>  
        /// 分页控件加载  
        /// </summary>  
        /// <param name="count">数据总行数</param>  
        private void DrawControl(int count)
        {
            m_recordCount = count;
            //设置DataNavigator  
            DrawControl(false);
            //设置LabControl  
            ShowLableInfo();
        }

        /// <summary>  
        /// 设置分页数据控件  
        /// </summary>  
        /// <param name="currentPageIndex">当前页</param>  
        /// <param name="pageSize2">每页显示行数</param>  
        /// <param name="recordTotal">总行数</param>  
        public void SetPageCtrl(int currentPageIndex, int pageSize2, int recordTotal)
        {
            pageIndex = currentPageIndex;
            recordCount = recordTotal;
            pageSize = pageSize2;

            DrawControl(recordTotal);
        }  
    }
}
